package ebj.common.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * FUNC DESCRIPTION -- I/O流工具类
 * @author MartinZhou
 * @email zhoushineyoung@hotmail.com
 * @date 2015-4-15
 */
public class StreamUtil {
	/**
	 * 功能摘要: 从输入流中读取数据，以字节数组返回
	 * @date-time 2015-4-9 下午9:49:58
	 * @param inputStream
	 * @return
	 * @throws IOException
	 */
	public static byte[] readBytesFromInputStream(InputStream inputStream) throws IOException {
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int length = 0;
		while ((length = inputStream.read()) != -1) {
			outputStream.write(buffer, 0, length);
		}
		inputStream.close();
		return outputStream.toByteArray();
	}
	
	public static String readStringFromInputStream(InputStream inputStream) {
		String result = "";
		// ByteArrayOutputStream相当于内存输出流
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int len = 0;
		// 将输入流转移到内存输出流中
		try {
			while ((len = inputStream.read(buffer, 0, buffer.length)) != -1) {
				out.write(buffer, 0, len);
			}
			// 将内存流转换为字符串
			result = new String(out.toByteArray());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
}
